package com.dk.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dk.data.Result;
import com.dk.data.ResultEnum;
import com.dk.pojo.item.TbItemCat;
import com.dk.service.item.ItemCatService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
@CrossOrigin
@Controller
@RequestMapping("itemCat")
public class ItemCatController {

    private final Logger logger = LoggerFactory.getLogger(ItemCatController.class);

    @Reference
    private ItemCatService itemCatService;

    @RequestMapping("toList")
    public String toList() {
        return "admin/item_cat";
    }

    @RequestMapping("listItemCatByPid/{parentId}")
    @ResponseBody
    public Result listItemCatByPid(@PathVariable("parentId") Long parentId){
        Map<String, Object> map = itemCatService.listItemCatByPid(parentId);
        Result result = Result.response(ResultEnum.OK).setData(map);
        return result;
    }

    @RequestMapping("saveItemCat")
    @ResponseBody
    public Result saveItemCat(TbItemCat itemCat) {
        try {
            int i = itemCatService.saveItemCat(itemCat);
            return Result.response(ResultEnum.OK);
        } catch (Exception e) {
            logger.error("商品分类添加错误",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    @RequestMapping("itemCatById/{id}")
    @ResponseBody
    public Result itemCatById(@PathVariable("id") Long id) {
        try {
            TbItemCat itemCat = itemCatService.itemCatById(id);
            return Result.response(ResultEnum.OK).setData(itemCat);

        } catch (Exception e) {
            logger.error("根据id查询商品分类：",e);
            return Result.response(ResultEnum.FAIL);
        }
    }
    @RequestMapping("updateItemCat")
    @ResponseBody
    public Result updateItemCat(TbItemCat itemCat) {
        try {
            int i = itemCatService.updateItemCat(itemCat);
            return Result.response(ResultEnum.OK);
        } catch (Exception e) {
            logger.error("商品分类修改错误",e);
            return Result.response(ResultEnum.FAIL);
        }
    }

    @RequestMapping("deleteItemCatByIds")
    @ResponseBody
    public Result deleteItemCatByIds(@RequestParam("ids[]")Long[] ids) {
        try {
            int i = itemCatService.deleteItemCatByIds(ids);
            return Result.response(ResultEnum.OK);
        } catch (Exception e) {
            logger.error("品牌删除错误:",e);
            return Result.response(ResultEnum.FAIL);
        }
    }
}
